package com.zhiwei.codesecurity.web.api.team.controller;

import com.zhiwei.codesecurity.common.base.NullObjectResp;
import com.zhiwei.codesecurity.common.base.ServiceResponse;
import com.zhiwei.codesecurity.common.utils.IdWorker;
import com.zhiwei.codesecurity.entity.Teams;
import com.zhiwei.codesecurity.service.teams.ITeamService;
import com.zhiwei.codesecurity.service.teams.response.AddTeamServRlt;
import com.zhiwei.codesecurity.service.teams.response.QueryTeamAndUserServListRlt;
import com.zhiwei.codesecurity.web.api.team.request.*;
import com.zhiwei.codesecurity.web.common.base.BaseController;
import com.zhiwei.codesecurity.web.common.base.BaseResponse;
import com.zhiwei.codesecurity.web.common.base.MapEntity;
import com.zhiwei.codesecurity.web.common.base.RequestHead;
import com.zhiwei.codesecurity.web.common.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/team")
public class TeamController extends BaseController {

    @Autowired
    private ITeamService teamService;

    /**
     * 查询团队
     * @param head
     * @param queryTeamReq
     * @param result
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public BaseResponse queryTeam(@RequestBody RequestHead head, @Valid @RequestBody QueryTeamReq queryTeamReq,
                                  BindingResult result) {
        logger.info(">>> process: 查询团队|teamId={}", queryTeamReq.getTeamId());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        ServiceResponse<Teams> serviceResponse = teamService.queryTeam(queryTeamReq.getTeamId());
        if (serviceResponse.isError()) {
            logger.warn("查询团队失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }
        MapEntity<String, String> rlt = new MapEntity<>();
        rlt.put("teamName", serviceResponse.getData().getTeamName());
        return ResponseUtil.getSuccessResponse(rlt);
    }

    /**
     * 添加团队
     * @param head
     * @param addTeamReq
     * @param result
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse addTeam(@RequestBody RequestHead head, @Valid @RequestBody AddTeamReq addTeamReq,
                                BindingResult result) {
        logger.info(">>> process: 添加团队|team_name={}", addTeamReq.getTeamName());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        Teams teams = new Teams();
        teams.setId(IdWorker.generate());
        teams.setTeamName(addTeamReq.getTeamName());
       /* teams.setParentId(addTeamReq.getParentTeamId());*/

        ServiceResponse<AddTeamServRlt> serviceResponse = teamService.addTeam(teams);
        if (serviceResponse.isError()) {
            logger.warn("团队添加失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }
        return ResponseUtil.getSuccessResponse(serviceResponse.getData());
    }

    /**
     * 修改团队
     * @param head
     * @param modifyTeamReq
     * @param result
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public BaseResponse modifyTeam(@RequestBody RequestHead head, @Valid @RequestBody ModifyTeamReq modifyTeamReq,
                                   BindingResult result) {
        logger.info(">>> process: 团队修改|team_name={}", modifyTeamReq.getTeamName());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        Teams teams = new Teams();
        teams.setId(modifyTeamReq.getTeamId());
        teams.setTeamName(modifyTeamReq.getTeamName());
        ServiceResponse<NullObjectResp> serviceResponse = teamService.modifyTeam(teams);
        if (serviceResponse.isError()) {
            logger.warn("团队修改失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }

        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 查询某个团队下的子团队和用户
     * @param head
     * @param queryTeamAndUserReq
     * @param result
     * @return
     */
    @RequestMapping("/query/teamanduser")
    @ResponseBody
    public BaseResponse QueryTeamAndUser(@RequestBody RequestHead head, @Valid @RequestBody QueryTeamAndUserReq queryTeamAndUserReq,
                                         BindingResult result) {
        logger.info(">>> process: 查询某个团队下的子团队和用户|team_id={}", queryTeamAndUserReq.getTeamId());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        ServiceResponse<QueryTeamAndUserServListRlt> serviceResponse =
                teamService.queryTeamAndUserByTeamId(String.valueOf(queryTeamAndUserReq.getTeamId()));

        if (serviceResponse.isError()) {
            logger.warn("查询某个团队下的子团队和用户失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }

        return ResponseUtil.getSuccessResponse(serviceResponse.getData());

    }

    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse deleteTeam(@RequestBody RequestHead head, @Valid @RequestBody DelTeamReq delTeamReq,
                                         BindingResult result) {
        logger.info(">>> process: 删除团队|teamId={}", delTeamReq.getTeamId());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        ServiceResponse<NullObjectResp> serviceResponse = teamService.deleteTeam(delTeamReq.getTeamId());
        if (serviceResponse.isError()) {
            logger.warn("删除团队失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }

        return ResponseUtil.getSuccessResponse();
    }
}
